package team.se.tms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import team.se.tms.annotation.Regex;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.dao.Dao;
import team.se.tms.dto.CourseDetail;
import team.se.tms.dto.CourseLabelDetail;
import team.se.tms.dto.Page;
import team.se.tms.dto.TeachingDetail;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.SelectCourseInfo;
import team.se.tms.po.StageInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.service.TeachCourseInfoService;
import team.se.tms.util.KeyUtil;
import team.se.tms.util.RandomUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 授课信息业务层，具体实现授课信息业务接口层定义的方法
 *
 * @author winkyle
 * @date 2018/12/7
 */
@Slf4j
@Service("TeachCourseInfoServiceImpl")
public class TeachCourseInfoServiceImpl extends BaseServiceImpl implements TeachCourseInfoService {
    @Resource(name = "DaoImpl")
    private Dao dao;
    /**
     * 根据用户id获取用户选课的课程详情
     *
     * @param userId 用户id
     * @param page 分页
     * @return 课程详情
     * @throws Exception 异常
     * @create on 2018/12/7 by winkyle
     */
    @Override
    public List<CourseDetail> listCourseDetails(@SpecialCheck("Pk")String userId, Page page) throws Exception {
        return dao.selectList("TeachCourseInfoMapper.selectCourseDetailsByUserId",getParameterPage(page,"userId",userId));
    }

    /**
     * 查询全部的授课详情
     *
     * @param page 分页
     * @return
     * @throws Exception
     * @create on 2018/12/8 by qingx
     */
    @Override
    public List<CourseDetail> listAllCourseDetails(Page page) throws Exception {
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%"+dao.selectList("TeachCourseInfoMapper.selectAllCourseDetails",page));
        List<CourseDetail> courseDetails = dao.selectList("TeachCourseInfoMapper.selectAllCourseDetails",page);
        for (CourseDetail courseDetail : courseDetails){
            if (courseDetail != null){
                String str = new String();
                List<StageInfo> stageInfos = courseDetail.getStageInfos();
                if(stageInfos != null) {
                    for (int i = 0; i < stageInfos.size() -1; i++) {
                        str += (stageInfos.get(i).getStageName() + "-");
                    }
                    str += stageInfos.get(stageInfos.size() -1).getStageName();
                    courseDetail.setStageChain(str);
                }
            }
        }
        return courseDetails;
    }

    /**
     * 查询修改课程时加载的信息
     *
     * @return
     * @throws Exception
     * @create on 2018/12/12 by qingx
     */
    @Override
    public TeachingDetail getModifyTeachingDetail(String teachCourseId) throws Exception {
        TeachingDetail teachingDetail = new TeachingDetail();
        Page page = new Page();
        teachingDetail.setTeacherInfos(dao.selectList("TeacherInfoMapper.selectAllTeacherInfos",page));
        teachingDetail.setLabelLibInfos(dao.selectList("LabelLibInfoMapper.selectAllLabelLibInfos",page));
        teachingDetail.setClassInfos(dao.selectList("StudentInfoMapper.selectAllClassInfos",page));
        teachingDetail.setStudentInfos(dao.selectList("StudentInfoMapper.selectAllStudentInfos",page));
        teachingDetail.setSelectCourseStudents(dao.selectList("StudentInfoMapper.selectStudentInfosByTeachCourseId",teachCourseId));
        return teachingDetail;
    }

    /**
     * 修改授课信息
     * @param teachCourseInfo
     * @param stuIds
     * @return
     * @throws Exception
     * @create on 2018/12/18 by qingx
     */
    @Override
    public boolean modifyTeachCourseInfo(TeachCourseInfo teachCourseInfo, String[] stuIds) throws Exception {
        int teachCourseResult = dao.update("TeachCourseInfoMapper.updateTeachCourseInfoByTeachCourseId",teachCourseInfo);
        if (teachCourseResult == 0){
            throw new UpdateException("授课信息修改失败");
        }
        int deleteResult = dao.delete("SelectCourseInfoMapper.deleteSelectCourseInfoByTeachCourseId",teachCourseInfo.getTeachCourseId());
        if (deleteResult == 0){
            throw new UpdateException("授课信息修改失败");
        }
        ArrayList<SelectCourseInfo> selectCourseInfos = new ArrayList<>();
        for(int i=0;i<stuIds.length;i++){
            SelectCourseInfo selectCourseInfo = new SelectCourseInfo();
            selectCourseInfo.setTeachCourseId(teachCourseInfo.getTeachCourseId());
            selectCourseInfo.setStuId(stuIds[i]);
            selectCourseInfos.add(selectCourseInfo);
        }
        int selectCourseResult =  dao.insert("SelectCourseInfoMapper.insertSelectCourseInfoByTeachCourseId",selectCourseInfos);
        if (selectCourseResult == 0){
            throw new UpdateException("授课信息修改失败");
        }

        //更新授课标签库
        Map<String,Object> map =new HashMap<>();
        map.put("teachCourseId",teachCourseInfo.getTeachCourseId());
        map.put("labelLibId",teachCourseInfo.getLabelLibId());
        map.put("result",-1);
        dao.update("TeachCourseInfoMapper.updateTeachCourseByTeachCourseIdAndLabelLibId",map);
        int result =(int)map.get("result");
        if(result==2){
            throw new UpdateException("当前标签库已经被使用,不允许修改");
            //1--修改成功，0--还是原来的标签库id
        }else if(result==-1){
            throw new UpdateException("授课信息修改失败");
        }
        return true;
    }

    /**
     * 查询创建课程时加载的信息
     *
     * @return
     * @throws Exception
     * @create on 2018/12/18 by qingx
     */
    @Override
    public TeachingDetail getCreateTeachingDetail() throws Exception {
        TeachingDetail teachingDetail = new TeachingDetail();
        Page page = new Page();
        teachingDetail.setTalentTrainingProgramInfos(dao.selectList("TalentTrainingProgramInfoMapper.selectTalentTrainingProgramInfos",page));
        teachingDetail.setCourseInfos(dao.selectList("CourseInfoMapper.selectAllCourseInfos",page));
        teachingDetail.setTeacherInfos(dao.selectList("TeacherInfoMapper.selectAllTeacherInfos",page));
        teachingDetail.setLabelLibInfos(dao.selectList("LabelLibInfoMapper.selectAllLabelLibInfos",page));
        teachingDetail.setStageChains(dao.selectList("StageInfoMapper.selectAllFirstStageId",page));
        teachingDetail.setClassInfos(dao.selectList("StudentInfoMapper.selectAllClassInfos",page));
        teachingDetail.setStudentInfos(dao.selectList("StudentInfoMapper.selectAllStudentInfos",page));
        return teachingDetail;
    }

    @Override
    public List<TeachCourseInfo> listTeachCourseInfos(String userId) throws Exception {
        return dao.selectList("TeachCourseMapper.selectTeachCourseInfosByTeaId ",userId);
    }

    /**
     * 添加授课信息
     * @param teachCourseInfo
     * @param stuIds
     * @return
     * @throws Exception
     * @create on 2018/12/27 by qingx
     */
    @Override
    public boolean saveTeachCourseInfo(TeachCourseInfo teachCourseInfo, String[] stuIds) throws Exception {
        teachCourseInfo.setTeachCourseId(KeyUtil.generatePrimaryKey());
        teachCourseInfo.setTeachCourseIcon(RandomUtil.generateRandomIcon());
        int teachCourseResult = dao.insert("TeachCourseInfoMapper.insertTeachCourseInfo",teachCourseInfo);
        if (teachCourseResult != 0){
            ArrayList<SelectCourseInfo> selectCourseInfos = new ArrayList<>();
            for (String stuId : stuIds) {
                SelectCourseInfo selectCourseInfo = new SelectCourseInfo();
                selectCourseInfo.setTeachCourseId(teachCourseInfo.getTeachCourseId());
                selectCourseInfo.setStuId(stuId);
                selectCourseInfos.add(selectCourseInfo);
            }
            int result = dao.insert("SelectCourseInfoMapper.insertSelectCourseInfoByTeachCourseId",selectCourseInfos);
            if (teachCourseResult != 0){
                return true;
            }
        }
        return false;
    }

    /**
     * 查询全部的授课信息
     *
     * @return
     * @throws Exception
     * @create on 2018/12/28 by qingx
     */
   @Override
    public List<TeachCourseInfo> listAllTeachCourseInfos(Page page) throws Exception {
        return dao.selectList("TeachCourseInfoMapper.selectAllTeachCourseInfos",page);
    }

    /**
     * 根据标签库id查询授课信息
     *
     * @param labelLibId 标签库id
     * @return List<TeachCourseInfo> 授课信息列表
     * @throws Exception 异常
     * @create on 2018/12/31 by johnny
     */
    @Override
    public List<TeachCourseInfo> listTeachCourseInfosByLabelLibId(String labelLibId) throws Exception {
        return dao.selectList("TeachCourseInfoMapper.selectTeachCourseInfosByLabelLibId",labelLibId);
    }

    /**
     * 根据授课id列表查询对应的标签及数量
     *
     * @param teachCourseIdList 授课id列表
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2019/1/3 by johnny
     */
    @Override
    public List<CourseLabelDetail> listTeachCourseLabelDetails(List<String> teachCourseIdList) throws Exception {
        return dao.selectList("TeachCourseInfoMapper.selectCourseLabelDetailByTeachCourseIds",teachCourseIdList);
    }


}
